if exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[vw_credited_po]') AND OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_credited_po]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

/***********************************************************************
* vw Name: dbo.vw_credited_po
* Created: 20110124, RGM
* Purpose: lista las PO que estan en Draft Discount, sus meses de credito
*	y las tasas que aplicaron para su calculo
*
* Modified: yyyymmdd, user, data
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
CREATE VIEW dbo.vw_credited_po AS
SELECT pov.purchase_order_version_id, COUNT(*) credit_months, bv.tax1, bv.tax2
	FROM purchases_orders_versions pov, po_balances_versions pbv, 
		vw_periods_versions pv, balances_versions bv
	WHERE pov.purchase_order_version_id = pbv.purchase_order_version_id
		AND pov.biweek_period_version_id = pv.biweek_period_version_id
		AND pv.month_period_version_id = bv.month_period_version_id
	GROUP BY pov.purchase_order_version_id, pv.month_period_version_id, 
		bv.tax1, bv.tax2

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

